package com.r2coding.ent.service.impl;

import com.github.pagehelper.PageHelper;
import com.r2coding.ent.dao.PictureDao;
import com.r2coding.ent.entity.PictureEntity;
import com.r2coding.ent.util.CommonUtil;
import com.r2coding.ent.vo.req.AlbumReqVo;
import com.r2coding.ent.vo.req.SavePictureReqVo;
import com.r2coding.ent.vo.resp.AlbumDetailRespVo;
import com.r2coding.ent.vo.resp.AlbumResp;
import com.r2coding.ent.vo.resp.AlbumRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Query;

import com.r2coding.ent.dao.AlbumDao;
import com.r2coding.ent.entity.AlbumEntity;
import com.r2coding.ent.service.AlbumService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 相册
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-06-20 19:59:36
 */
@Service("albumService")
public class AlbumServiceImpl extends ServiceImpl<AlbumDao, AlbumEntity> implements AlbumService {

    @Autowired
    private PictureDao pictureDao;

    @Autowired
    CommonUtil commonUtil;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AlbumEntity> page = this.page(
                new Query<AlbumEntity>().getPage(params),
                new QueryWrapper<AlbumEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public AlbumResp getAll(AlbumReqVo req) {
        AlbumResp albumResp = new AlbumResp();
        // 查询满足条件的总数
        QueryWrapper<AlbumEntity> wrapper1 = new QueryWrapper<>();
        if (!StringUtils.isEmpty(req.getName())) {
            wrapper1.like("name", req.getName());
        }
        Integer count = this.baseMapper.selectCount(wrapper1);
        albumResp.setTotal(count);

        PageHelper.startPage(req.getCurrent(), req.getSize());
        QueryWrapper<AlbumEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(req.getName())) {
            queryWrapper.like("name", req.getName());
        }
        queryWrapper.orderByDesc("create_time");
        List<AlbumEntity> albumEntityList = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(albumEntityList)) {
            return null;
        }
        List<AlbumRespVo> list = albumEntityList.stream().map(album -> {
            AlbumRespVo respVo = new AlbumRespVo();
            BeanUtils.copyProperties(album, respVo);
            QueryWrapper<PictureEntity> wrapper = new QueryWrapper<PictureEntity>();
            wrapper.eq("album_id", album.getId());
            List<PictureEntity> pictureEntities = pictureDao.selectList(wrapper);
            if (!CollectionUtils.isEmpty(pictureEntities)) {
                respVo.setPictures(pictureEntities);
            }
            respVo.setPictureTotal(pictureEntities.size());
            return respVo;
        }).collect(Collectors.toList());
        albumResp.setAlbumRespVos(list);
        return albumResp;
    }

    @Override
    public AlbumDetailRespVo getAlbumInfo(Integer id) {
        AlbumDetailRespVo respVo = new AlbumDetailRespVo();
        AlbumEntity album = this.baseMapper.selectById(id);
        BeanUtils.copyProperties(album, respVo);
        QueryWrapper<PictureEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("album_id", id);
        List<PictureEntity> list = pictureDao.selectList(wrapper);
        respVo.setPictureCount(list.size());
        respVo.setSurplus(respVo.getSize() - list.size());
        return respVo;
    }

    @Override
    public void savePicture(SavePictureReqVo req) {
        for (String url : req.getUrlList()) {
            // 检查该相册的容量是否满足
            commonUtil.checkAlbumSize(req.getAlbumId());
            QueryWrapper<PictureEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("url", url);
            wrapper.eq("album_id", req.getAlbumId());
            // 检查是否存在相同的照片
            Integer count = pictureDao.selectCount(wrapper);
            if (count > 0) {
                continue;
            }

            PictureEntity picture = new PictureEntity();
            picture.setAlbumId(req.getAlbumId());
            picture.setUrl(url);
            picture.setDeleted(0);
            picture.setCreateTime(new Date());
            pictureDao.insert(picture);
        }
    }

    @Override
    public List<AlbumEntity> getAlbumList(Integer albumId) {
        List<AlbumEntity> list = this.baseMapper.selectList(null);
        list.removeIf(albumEntity -> albumEntity.getId() == albumId);
        return list;
    }

    @Override
    public AlbumResp getPublicAlbum(AlbumReqVo req) {
        AlbumResp albumResp = new AlbumResp();
        // 查询满足条件的总数
        QueryWrapper<AlbumEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("is_public", 1);
        Integer count = this.baseMapper.selectCount(wrapper1);
        albumResp.setTotal(count);

        PageHelper.startPage(req.getCurrent(), req.getSize());
        QueryWrapper<AlbumEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<AlbumEntity> albumEntityList = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(albumEntityList)) {
            return null;
        }
        List<AlbumRespVo> list = albumEntityList.stream().map(album -> {
            AlbumRespVo respVo = new AlbumRespVo();
            BeanUtils.copyProperties(album, respVo);
            QueryWrapper<PictureEntity> wrapper = new QueryWrapper<PictureEntity>();
            wrapper.eq("album_id", album.getId());
            List<PictureEntity> pictureEntities = pictureDao.selectList(wrapper);
            if (!CollectionUtils.isEmpty(pictureEntities)) {
                respVo.setPictures(pictureEntities);
            }
            respVo.setPictureTotal(pictureEntities.size());
            return respVo;
        }).collect(Collectors.toList());
        albumResp.setAlbumRespVos(list);
        return albumResp;
    }
}